home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: hearst.acc.Virginia.EDU!maxwell!gcl8a
- From: gcl8a@Virginia.EDU (Gregory Carl Lewin)
- Subject: type-casting in multi-inheritance
- Message-ID: <DMn7Ix.G9@Virginia.EDU>
- Organization: University of Virginia
- Date: Mon, 12 Feb 1996 03:11:20 GMT
-
- I am a bit fuzzy about casting from pointers to derived objects
- to base classes when you are using multiple inheritance.
- Perhaps someone could explain the following to me. Consider
- the arrangement:
-
- class Base1 {};
- class Base2 {};
- class Derived12 : public Base1, Base2 {};
-
- Base1 b1;
- Base2 b2;
- Derived12 d12;
-
- void foo(void) {
- Base1* pb1;
- pb1=&b1;
- pb1=&d12;
-
- Base2* pb2;
- pb2=&b2;
- pb2=&d12; //ERROR: Cannot convert pointer
- pb2=(Base2*) &d12; //OK: But is it ALWAYS safe?
-
- }
-
- Because Derived 12 is publicly derived from Base1 and Base2, I
- would have expected the conversion of &d12 to a Base2* to be
- automatic; however, the comiler balks. The subsequent explicit
- conversion works, and the program seems to act appropriately,
- but using the explicit type-cast scares me. Can someone
- explain why this is and if this is the "correct" way around the
- problem (alternatives being virtual inheritance with class Base
- being a base for Base1 and Base2, and ???).
-
- Greg Lewin
-